home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / CONSTANT.JAV < prev    next >
Encoding:
Text File  |  1997-02-26  |  8.2 KB  |  345 lines

  1. /*
  2.  * @(#)ConstantPoolInfo.java    1.5 95/08/16 Chuck McManis
  3.  *
  4.  * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies.
  10.  *
  11.  * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
  12.  * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  13.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  14.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE
  15.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  16.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  17.  */
  18.  
  19. package util;
  20.  
  21. import java.io.DataInputStream;
  22. import java.io.DataOutputStream;
  23. import java.io.IOException;
  24.  
  25. /**
  26.  * This class defines an entry in the constant pool for a Java class.
  27.  * The class file is primarily composed of ConstantPool entries and
  28.  * manipulation is done by modifying those entries.
  29.  *
  30.  * @version     1.5, 16 Aug 1995
  31.  * @author    Chuck McManis
  32.  * @see        ClassFile
  33.  */
  34.  
  35. public class ConstantPoolInfo{
  36.     int    type;            // type of this item
  37.     String name;         // String for the type
  38.     ConstantPoolInfo  arg1;    // index to first argument
  39.     ConstantPoolInfo  arg2;    // index to second argument
  40.     short index1, index2;
  41.     String     strValue;         // ASCIZ String value
  42.     int        intValue;
  43.     long    longValue;
  44.     float    floatValue;
  45.     double    doubleValue;
  46.  
  47.     public static final int CLASS = 7;
  48.     public static final int FIELDREF = 9;
  49.     public static final int METHODREF = 10;
  50.     public static final int STRING = 8;
  51.     public static final int INTEGER = 3;
  52.     public static final int FLOAT = 4;
  53.     public static final int LONG = 5;
  54.     public static final int DOUBLE = 6;
  55.     public static final int INTERFACE = 11;
  56.     public static final int NAMEANDTYPE = 12;
  57.     public static final int ASCIZ = 1;
  58.     public static final int UNICODE = 2;
  59.  
  60.  
  61.     /**
  62.      * Construct a new ConstantPoolInfo object that is of type ASCIZ
  63.      */
  64.     public ConstantPoolInfo(String value) {
  65.     index1 = -1;
  66.     index2 = -1;
  67.     arg1 = null;
  68.     arg2 = null;
  69.     type = ASCIZ;
  70.     strValue = value;
  71.     }
  72.  
  73.     /**
  74.      * Construct a new ConstantPoolInfo object that is of type INTEGER
  75.      */
  76.     public ConstantPoolInfo(int value) {
  77.     index1 = -1;
  78.     index2 = -1;
  79.     arg1 = null;
  80.     arg2 = null;
  81.     type = INTEGER;
  82.     intValue = value;
  83.     }
  84.  
  85.     /**
  86.      * Construct a new ConstantPoolInfo object that is of type FLOAT
  87.      */
  88.     public ConstantPoolInfo(float value) {
  89.     index1 = -1;
  90.     index2 = -1;
  91.     arg1 = null;
  92.     arg2 = null;
  93.     type = FLOAT;
  94.     floatValue = value;
  95.     }
  96.  
  97.     /**
  98.      * Construct a new ConstantPoolInfo object that is of type LONG
  99.      */
  100.     public ConstantPoolInfo(long value) {
  101.     index1 = -1;
  102.     index2 = -1;
  103.     arg1 = null;
  104.     arg2 = null;
  105.     type = LONG;
  106.     longValue = value;
  107.     }
  108.  
  109.     /**
  110.      * Construct a new ConstantPoolInfo object that is of type DOUBLE
  111.      */
  112.     public ConstantPoolInfo(double value) {
  113.     index1 = -1;
  114.     index2 = -1;
  115.     arg1 = null;
  116.     arg2 = null;
  117.     type = DOUBLE;
  118.     doubleValue = value;
  119.     }
  120.  
  121.     /**
  122.      * Generic constructor
  123.      */
  124.     public ConstantPoolInfo() {
  125.     index1 = -1;
  126.     index2 = -1;
  127.     arg1 = null;
  128.     arg2 = null;
  129.     type = -1;
  130.     }
  131.  
  132.     /**
  133.      * return the type of this constant pool item.
  134.      */
  135.     public int isType() {
  136.     return (type);
  137.     }
  138.  
  139.     public boolean read(DataInputStream dis)
  140.     throws IOException {
  141.     int    len;
  142.     char    c;
  143.  
  144.     type = dis.readByte();
  145.     switch (type) {
  146.         case CLASS:
  147.         name = "Class";
  148.         index1 = dis.readShort();
  149.         index2 = -1;
  150.         break;
  151.         case FIELDREF:
  152.         name = "Field Reference";
  153.         index1 = dis.readShort();
  154.         index2 = dis.readShort();
  155.         break;
  156.         case METHODREF:
  157.         name = "Method Reference";
  158.         index1 = dis.readShort();
  159.         index2 = dis.readShort();
  160.         break;
  161.         case INTERFACE:
  162.         name = "Interface Method Reference";
  163.         index1 = dis.readShort();
  164.         index2 = dis.readShort();
  165.         break;
  166.         case NAMEANDTYPE:
  167.         name = "Name and Type";
  168.         index1 = dis.readShort();
  169.         index2 = dis.readShort();
  170.         break;
  171.         case STRING:
  172.         name = "String";
  173.         index1 = dis.readShort();
  174.         index2 = -1;
  175.         break;
  176.         case INTEGER:
  177.         name = "Integer";
  178.         intValue = dis.readInt();
  179.         break;
  180.         case FLOAT:
  181.         name = "Float";
  182.         floatValue = dis.readFloat();
  183.         break;
  184.         case LONG:
  185.         name = "Long";
  186.         longValue = dis.readLong();
  187.         break;
  188.         case DOUBLE:
  189.         name = "Double";
  190.         doubleValue = dis.readDouble();
  191.         break;
  192.         case ASCIZ:
  193.         case UNICODE:
  194.         if (type == ASCIZ)
  195.             name = "ASCIZ";
  196.         else
  197.             name = "UNICODE";
  198.  
  199.         StringBuffer xxBuf = new StringBuffer();
  200.  
  201.         len = dis.readShort();
  202.         while (len > 0) {
  203.             c = (char) (dis.readByte());
  204.             xxBuf.append(c);
  205.             len--;
  206.         }
  207.         strValue = xxBuf.toString();
  208.         break;
  209.         default:
  210.         System.out.println("Warning bad type.");
  211.     }
  212.     return (true);
  213.     }
  214.  
  215.     public void write(DataOutputStream dos, ConstantPoolInfo pool[])
  216.     throws IOException, Exception {
  217.     dos.write(type);
  218.     switch (type) {
  219.         case CLASS:
  220.         case STRING:
  221.         dos.writeShort(indexOf(arg1, pool));
  222.         break;
  223.         case FIELDREF:
  224.         case METHODREF:
  225.         case INTERFACE:
  226.         case NAMEANDTYPE:
  227.         dos.writeShort(indexOf(arg1, pool));
  228.         dos.writeShort(indexOf(arg2, pool));
  229.         break;
  230.         case INTEGER:
  231.         dos.writeInt(intValue);
  232.         break;
  233.         case FLOAT:
  234.         dos.writeFloat(floatValue);
  235.         break;
  236.         case LONG:
  237.         dos.writeLong(longValue);
  238.         break;
  239.         case DOUBLE:
  240.         dos.writeDouble(doubleValue);
  241.         break;
  242.         case ASCIZ:
  243.         case UNICODE:
  244.         dos.writeShort(strValue.length());
  245.         dos.writeBytes(strValue);
  246.         break;
  247.         default:
  248.         throw new Exception("ConstantPoolInfo::write() - bad type.");
  249.     }
  250.     }
  251.  
  252.     public String toString() {
  253.     StringBuffer s;
  254.  
  255.     if (type == ASCIZ) {
  256.         return(strValue);
  257.     }
  258.  
  259.     if (type == INTEGER) {
  260.         return("= "+intValue);
  261.     }
  262.  
  263.     if (type == LONG) {
  264.         return("= "+longValue);
  265.     }
  266.  
  267.     if (type == FLOAT) {
  268.         return("= "+floatValue);
  269.     }
  270.  
  271.     if (type == DOUBLE) {
  272.         return("= "+doubleValue);
  273.     }
  274.  
  275.     s = new StringBuffer();
  276.     s.append(name);
  277.     s.append(":");
  278.     if (arg1 != null)
  279.         s.append(arg1.toString());
  280.     else if (index1 != -1)
  281.         s.append("I1["+index1+"], ");
  282.     if (arg2 != null)
  283.         s.append(arg2.toString());
  284.     else if (index2 != -1)
  285.         s.append("I2["+index2+"], ");
  286.     return (s.toString());
  287.     }
  288.  
  289.     public static short indexOf(ConstantPoolInfo item,
  290.                     ConstantPoolInfo pool[])
  291.     throws Exception {
  292.     for (int i = 0; i < pool.length; i++) {
  293.         if (item == pool[i])
  294.         return (short) i;
  295.     }
  296.     throw new Exception("ConstantPoolInfo:: indexOf() - item not in pool.");
  297.     }
  298.  
  299.     /**
  300.      * Returns true if these constant pool items are identical.
  301.      */
  302.     public boolean isEqual(ConstantPoolInfo cp) {
  303.     if (cp == null)
  304.         return false;
  305.  
  306.     if (cp.type != type)
  307.         return (false);
  308.     switch (cp.type) {
  309.         case CLASS:
  310.         case STRING:
  311.         return (arg1 == cp.arg1);
  312.         case FIELDREF:
  313.         case METHODREF:
  314.         case INTERFACE:
  315.         case NAMEANDTYPE:
  316.         return ((arg1 == cp.arg1) && (arg2 == cp.arg2));
  317.         case INTEGER:
  318.         return (cp.intValue == intValue);
  319.         case FLOAT:
  320.         return (cp.floatValue == floatValue);
  321.         case LONG:
  322.         return (cp.longValue == longValue);
  323.         case DOUBLE:
  324.         return (cp.doubleValue == doubleValue);
  325.         case ASCIZ:
  326.         case UNICODE:
  327.         return (cp.strValue.compareTo(strValue) == 0);
  328.     }
  329.     return (false);
  330.     }
  331.  
  332.     /**
  333.      * Returns the reference to the constant pool item that is
  334.      * already in pool, that matches this one.
  335.      */
  336.     public ConstantPoolInfo inPool(ConstantPoolInfo pool[]) {
  337.     for (int i = 1; i < pool.length; i++) {
  338.         if (isEqual(pool[i]))
  339.         return (pool[i]);
  340.     }
  341.     return null;
  342.     }
  343.  
  344. }
  345.